/**
 * Controller of the sketelon
 * play the role as organize the models/data with views
 */
function ECtrl(options) {
    this.$options = options;
    var result = eval(this.$options.data);
    var data = this._data = result;
    var me = this;
    Object.keys(data).forEach(function (key) {
        me._proxy(key);
    });
    observe(data, this);
    this.$compile = new Compile(options.view || document.body, this);
}

ECtrl.prototype = {
    $watch: function (key, cb, options) {
        new Watcher(this, key, cb);
    },
    _proxy: function (key) {
        var me = this;
        Object.defineProperty(me, key, {
            configurable: true,
            enumerable: true,
            get: function proxyGetter() {
                return me._data[key];
            },
            set: function proxySetter(newVal) {
                me._data[key] = newVal;
            }
        });
    },
    extend: function (_options) {
        var opt = this.$options;
        Object.keys(opt).forEach(function (key) {
            if (_options[key] === undefined) {
                _options[key] = opt[key];
            }
        });

        function beget(obj) {
            var F = function () {
            };
            F.prototype = obj;
            return new F();
        }

        function Sub() {
            ECtrl.call(this, _options);
        }

        var proto = beget(ECtrl.prototype);
        proto.constructor = Sub;
        Sub.prototype = proto;
        return new Sub(_options);
    }
};
